package org.nokatag.tagjava;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.nokatag.password.AESPlus;
import org.nokatag.system.Concest;
import org.nokatag.system.Function;

/**
 * <p>@Title ajax输入</p>
 * <p>@Description </p>
 * <p>@Version 1.0.0</p>
 * <p>@author rebin</p>
 * <p>@date 2016年8月10日</p>
 * <p>xiefangjian@163.com</p>
 * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
 */
public class AjaxInputText extends Inputsuper{
	private static final long serialVersionUID = 4975030788591813491L;
	private String mesg = "erro mesages for input!";
	private String allownull="true";
	private String ismore = null;//是否多选
	private String sql = null;
	private String vsql = null;//值sql
	private String length = null;//最小查询长度
	private String ondelete = null;//在删除选项时调用的js方法
	
	private String dataurl = null;//数据加载地址
	private String valueurl = null;//值转换成显示值的地址
	
	public int doStartTag()throws JspException{
		JspWriter jspOut = pageContext.getOut();
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		String basePath = basepath(request);
		String pwidth = getWidth();
		pwidth =LenFormat((pwidth==null)?"120":pwidth);
		if(null!=dataurl){
			sql = "test";
		}
		if(null!=valueurl){
			vsql = "test";
		}
		NOOUT.put("width", true);
		NOOUT.put("id", true);
		NOOUT.put("value", true);
		NOOUT.put("name", true);
		NOOUT.put("onchange", true);
		Integer swidth = Integer.parseInt(pwidth.substring(0,pwidth.length()-"px".length()))-0;
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append(potjs(request));//加载Prototype
		stringBuilder.append(ajaxinputTextJS(request));//加载Prototype
		FormHtmlInsertid(getId(),getName(),getValue());
		stringBuilder.append("<div style=\"top:0px;left:0px;display: none;position: absolute;z-index:99999;overflow: auto;border: 1px solid #AFAFAF;background-color:#FFFFFF;\"  id=\""+getId()+"_selectbody\"></div>");
		stringBuilder.append("<div style=\"float: left;\">");
		stringBuilder.append("<input type=\"hidden\" name=\""+getName()+"\" id=\""+getId()+"\" value=\""+(null==getValue()?"":getValue())+"\"/>");
		stringBuilder.append("<div style=\"float: left;display: none;\"  id=\""+getId()+"_multinput\"></div>");
		stringBuilder.append("<div id=\""+getId()+"_sdDiv\" class=\"nk_input_lineheight nk_input_sdDiv\" style=\"width:"+pwidth+";\">");
		stringBuilder.append("<input type=\"text\" id=\""+getId()+"_input\" class=\"nk_input_lineheight nk_input_text\"  style=\"width:"+swidth+"px\" ");
		stringBuilder.append(getBaseStr()+"/></div>");
		if("false".equalsIgnoreCase(allownull)){
			stringBuilder.append("<div style=\"color: red;float: left;\" class=\"nk_input_lineheight\">*</div>");
		}
		stringBuilder.append("<img id=\""+getId()+"_img\" style=\"float: left;\" class=\"nk_input_imgheight\"/><div class=\"nk_input_message\" nowrap id=\""+getId()+"_div\" style=\"position:absolute;display:none;\">"+mesg+"</div>");
		stringBuilder.append("</div>");
		stringBuilder.append("<script type=\"text/javascript\">");
		stringBuilder.append("Event.observe(document, 'dom:loaded', function() {");
		stringBuilder.append("var cfgs"+getId()+" = {id:'"+getId()+"',allownull:"+allownull+",rurl:'"+basePath+"'");
		stringBuilder.append(",ndataLang:'"+Function.lung(request,"org_nokatag_grid_25")+"'");
		if(null!=getReadonly()){
			stringBuilder.append(",readonly:"+("readonly".equals(getReadonly()))+"");//是否只读
		}
		if(null!=getDisabled()){
			stringBuilder.append(",disabled:"+("disabled".equals(getDisabled()))+"");//是否禁用
		}
		if(null!=ismore){
			stringBuilder.append(",ismore:"+("true".equals(ismore))+"");
		}
		if(null!=sql){
			stringBuilder.append(",sql:'"+AESPlus.encrypt(sql, Concest.SQLKEY)+"'");
		}
		if(null!=vsql){
			stringBuilder.append(",vsql:'"+AESPlus.encrypt(vsql, Concest.SQLKEY)+"'");
		}
		if(null!=length){
			stringBuilder.append(",qulenght:"+length+"");
		}
		if(null!=ondelete){
			stringBuilder.append(",ondelete:function(a){return "+ondelete+"(a);}");
		}
		if(null!=getOnchange()){
			stringBuilder.append(",onchange:function(a){return "+getOnchange()+"(a);}");
		}
		if(null!=dataurl){
			stringBuilder.append(",dataurl:'"+dataurl+"'");
		}
		if(null!=valueurl){
			stringBuilder.append(",valueurl:'"+valueurl+"'");
		}
		stringBuilder.append("};");
		stringBuilder.append("var np"+getId()+" = new ajaxinputtext(cfgs"+getId()+");");
		stringBuilder.append("np"+getId()+".initinput();");
		stringBuilder.append("});");
		stringBuilder.append("</script>");
		try {
			jspOut.write(stringBuilder.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}
	/**
	 * 去掉换行符
	 * @param str
	 * @return
	 */
	public static String delEn(String str){
		//-----------------去掉换行符--------------------------------------
		Pattern p = Pattern.compile("\\s*\n");
		Matcher m = p.matcher(str);
		str=m.replaceAll("").trim();
		return str;
		//----------------------------------------------------------------
	}
	
	public String getMesg() {
		return mesg;
	}
	public void setMesg(String mesg) {
		this.mesg = mesg;
	}
	public String getAllownull() {
		return allownull;
	}
	public void setAllownull(String allownull) {
		this.allownull = allownull;
	}
	public String getSql() {
		return sql;
	}
	public void setSql(String sql) {
		this.sql = sql;
	}
	public String getIsmore() {
		return ismore;
	}
	public void setIsmore(String ismore) {
		this.ismore = ismore;
	}
	public String getVsql() {
		return vsql;
	}
	public void setVsql(String vsql) {
		this.vsql = vsql;
	}
	public String getLength() {
		return length;
	}
	public void setLength(String length) {
		this.length = length;
	}
	public String getOndelete() {
		return ondelete;
	}
	public void setOndelete(String ondelete) {
		this.ondelete = ondelete;
	}
	public String getDataurl() {
		return dataurl;
	}
	public void setDataurl(String dataurl) {
		this.dataurl = dataurl;
	}
	public String getValueurl() {
		return valueurl;
	}
	public void setValueurl(String valueurl) {
		this.valueurl = valueurl;
	}
	
	
}